package com.wenwan.manage.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.wenwan.manage.mapper.UserWeiXinMapper;
import com.wenwan.manage.pojo.UserWeiXin;
import com.wenwan.manage.pojo.WeiXinOpenId;
import com.wenwan.manage.service.UserWeiXinService;
import com.wenwan.manage.setting.DefaultValue;
import com.wenwan.manage.setting.ParameterSetting;
import com.wenwan.manage.setting.RegExpSetting;
import com.wenwan.manage.setting.StatusSetting;
import com.wenwan.manage.setting.WeiXinSetting;
import com.wenwan.manage.util.SSLClient;
import com.wenwan.manage.util.Util;

@Service
public class UserWeiXinServiceImpl implements UserWeiXinService {

	@Autowired
	private UserWeiXinMapper userWeiXinMapper;

	@Override
	public Map<String, Object> getOpenIdByCode(String code) {
		// TODO Auto-generated method stub
		// DataToObject<WeiXinOpenId> dto = new DataToObject<WeiXinOpenId>();
		Map<String, Object> dto = new HashMap<String, Object>();

		String httpOrgCreateTest = WeiXinSetting.JS_CODE_2_SESSION;
		Map<String, String> createMap = new HashMap<String, String>();
		createMap.put(ParameterSetting.APPID, WeiXinSetting.APPID);
		createMap.put(ParameterSetting.SECRET, WeiXinSetting.SECRET);
		createMap.put(ParameterSetting.JS_CODE, code);
		createMap.put(ParameterSetting.GRANT_TYPE, WeiXinSetting.GRANT_TYPE);
		String httpOrgCreateTestRtn = doPost(httpOrgCreateTest, createMap, DefaultValue.CHARSET_UTF8);
		System.out.println("result:" + httpOrgCreateTestRtn);

		WeiXinOpenId data = new Gson().fromJson(httpOrgCreateTestRtn, WeiXinOpenId.class);
		// dto.setData(data);
		dto.put("data", data);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

	@Override
	public Map<String, Object> create(String openid, String userId) {
		// TODO Auto-generated method stub
		// DataToObject<Boolean> dto = new DataToObject<Boolean>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (userId != null && !userId.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "userId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("userId" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", openid.toUpperCase());
		map.put("createTime", Util.getTime());
		map.put("updateTime", Util.getTime());
		map.put("openid", openid);
		map.put("userId", userId);

		Boolean data = userWeiXinMapper.create(map);
		// dto.setData(data);
		dto.put("data", data);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

	@Override
	public Map<String, Object> selectByOpenid(String openid) {
		// TODO Auto-generated method stub
		// DataToObject<UserWeiXin> dto = new DataToObject<UserWeiXin>();
		Map<String, Object> dto = new HashMap<String, Object>();

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", openid.toUpperCase());

		UserWeiXin data = userWeiXinMapper.selectByOpenid(map);
		// dto.setData(data);
		dto.put("data", data);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

	public String doPost(String url, Map<String, String> map, String charset) {
		HttpClient httpClient = null;
		HttpPost httpPost = null;
		String result = null;
		try {
			httpClient = new SSLClient();
			httpPost = new HttpPost(url);
			// 设置参数
			List<NameValuePair> list = new ArrayList<NameValuePair>();
			Iterator<Entry<String, String>> iterator = map.entrySet().iterator();
			while (iterator.hasNext()) {
				Entry<String, String> elem = iterator.next();
				list.add(new BasicNameValuePair(elem.getKey(), elem.getValue()));
			}
			if (list.size() > 0) {
				UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, charset);
				httpPost.setEntity(entity);
			}
			HttpResponse response = httpClient.execute(httpPost);
			if (response != null) {
				HttpEntity resEntity = response.getEntity();
				if (resEntity != null) {
					result = EntityUtils.toString(resEntity, charset);
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return result;
	}

}
